package com.lkx.farmershop.controller;

import com.lkx.farmershop.common.api.ApiResult;
import com.lkx.farmershop.model.dto.ChatDto;
import com.lkx.farmershop.model.dto.PrivateChatDto;
import com.lkx.farmershop.model.vo.ChatVo;
import com.lkx.farmershop.model.vo.PrivateChatVo;
import com.lkx.farmershop.model.vo.SimpleChatVo;
import com.lkx.farmershop.service.ChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/chat")
public class ChatController {

    @Autowired
    private ChatService chatService;

    /**
     * 群聊发送消息
     * @param chatDto
     * @return
     */
    @Transactional
    @PostMapping("/sendMessage")
    public ApiResult<String> sendMessage(@RequestBody ChatDto chatDto) {
        return chatService.sendMessage(chatDto);
    }

    /**
     * 私聊发送消息
     * @param chatDto
     * @return
     */
    @Transactional
    @PostMapping("/sendPrivateMessage")
    public ApiResult<String> sendPrivateMessage(@RequestBody PrivateChatDto chatDto) {
        return chatService.sendPrivateMessage(chatDto);
    }

    /**
     * 获取群聊聊天列表
     * @param tuanId
     * @return
     */
    @GetMapping("/getChatList")
    public ApiResult<List<ChatVo>> getChatList(@RequestParam("tuanId") Long tuanId) {
        return chatService.getChatList(tuanId);
    }


    /**
     * 获取私聊聊天列表
     * @param userId
     * @param theUserId
     * @return
     */
    @GetMapping("/getPrivateChat")
    public ApiResult<List<ChatVo>> getPrivateChat(@RequestParam("userId") Long userId, @RequestParam("theUserId") Long theUserId) {
        return chatService.getPrivateChat(userId, theUserId);
    }

    /**
     * 获取所有聊天列表
     * @param userId
     * @return
     */
    @GetMapping("/getAllChat")
    public ApiResult<List<SimpleChatVo>> getAllChat(@RequestParam("userId") Long userId) {
        return chatService.getAllChat(userId);
    }

    @GetMapping("/getAllPrivateChat")
    public ApiResult<List<PrivateChatVo>> getAllPrivateChat(@RequestParam("userId") Long userId) {
        return chatService.getAllPrivateChat(userId);
    }
}
